#include <stdio.h>
#include <string.h>
#include "dicadt.h"
#include "extras.h"
#include "CyS_TP2.h"

static void AppendBothNums (char * word, int num1, char * answer, int num2);
static void AppendLeftNums (char * word, int num1, char * answer);
static void AppendRightNums (char * word, char * answer, int num2);
void cleanWord (char * word);


void AppendBoth (char * word, char * word2, int len, int * flag, unsigned char * possible_pass, unsigned char * u_name, unsigned char * dic_word, unsigned char * hex_dump, unsigned char * e_pass, char * enc_txt)
{
	int i,j,MAX;
	 
	if (len == 2 || len == 3 || len == 4)
		MAX = 99;
	else if(len == 5 || len == 6)
		MAX = 9;
	else
		MAX = 0;
	
	for (i=0; i < 9; i++){
		for (j=0; j < 9 ; j++){
			cleanWord (word2);
			AppendBothNums (word, i, word2, j);
			
			/* TODO: aca probar con word */
			if (word2 != NULL){
				if ((*flag = doIt (possible_pass, u_name, word2, hex_dump, e_pass, enc_txt)) == 0)
					return ;
			}
			else
				return ;
		}
	}	
}

void AppendLeft(char * word, char * word2, int len, int * flag, unsigned char * possible_pass, unsigned char * u_name, unsigned char * dic_word, unsigned char * hex_dump, unsigned char * e_pass, char * enc_txt)
{
	int i,MAX;
	 
	if (len == 2 || len == 3 || len == 4 || len == 5 || len == 6)
		MAX = 99;
	else if (len == 7)
		MAX = 9;
	else
		MAX = 0;

	for (i=0; i < 9; i++){
		cleanWord (word2);
		AppendLeftNums (word, i, word2);
			
		if (word2 != NULL){
			if ((*flag = doIt (possible_pass, u_name, word2, hex_dump, e_pass, enc_txt)) == 0)
				return ;
		}
		else
			return ;
	}	
}

void AppendRight (char * word, char * word2, int len, int * flag, unsigned char * possible_pass, unsigned char * u_name, unsigned char * dic_word, unsigned char * hex_dump, unsigned char * e_pass, char * enc_txt)
{
	int i,j,MAX;
	 
	if (len == 2 || len == 3 || len == 4 || len == 5 || len == 6)
		MAX = 99;
	else if (len == 7)
		MAX = 9;
	else
		MAX = 0;
	
	for (i=0; i < 9; i++){
		cleanWord (word2);
		AppendRightNums (word, word2, i);
			
		if (word2 != NULL){
			if ((*flag = doIt (possible_pass, u_name, word2, hex_dump, e_pass, enc_txt)) == 0)
				return ;
		}
		else
			return;
	}	
}

static void AppendBothNums (char * word, int num1, char * answer, int num2)
{
	char snum1[3]={0};
	char snum2[3]={0};
	
	if (num1 > 99 || num2 > 99 || num1 < 0 || num2 < 0){
		answer = NULL;
		return ;
	}
	
	sprintf (snum1, "%d", num1);
	sprintf (snum2, "%d", num2);
	
	strcat (answer, snum1);
	strncat (answer, word, MAX_WORD_LENGTH - (strlen(snum1) + strlen(snum2)));
	strcat (answer, snum2);
	
	return;
}

static void AppendLeftNums (char * word, int num1, char * answer)
{
	char snum1[3]={0};
	char snum2[3]={0};
	
	if (num1 > 99 || num1 < 0 ){
		answer = NULL;
		return ;
	}
	
	sprintf (snum1, "%d", num1);
	
	strcat (answer, snum1);
	strncat (answer, word, MAX_WORD_LENGTH - (strlen(snum1) + strlen(snum2)));
}
static void AppendRightNums (char * word, char * answer, int num2)
{
	char snum1[3]={0};
	char snum2[3]={0};
	
	if (num2 > 99 || num2 < 0){
		answer = NULL;
		return ;
	}
	
	sprintf (snum2, "%d", num2);
	
	strncat (answer, word, MAX_WORD_LENGTH - (strlen(snum1) + strlen(snum2)));
	strcat (answer, snum2);

	
	return;	
}

void
cleanWord (char * word)
{
	int i;
	for (i=0; i < 10; i++){
		word[i]=0;
	}
	return ;
}
